<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
  request.setAttribute("now",new Date());
%>

<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">发文申请表</h3>
            </div>

            <div style="margin: 10px">

                <!-- Nav tabs -->
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#bsc-tab-1" data-toggle="tab">发文申请表</a></li>
                    <c:if test="${outfile.processState=='Finished'}">
                        <li><a href="#bsc-tab-2" data-toggle="tab">发文修订</a></li>
                    </c:if>
                </ul>

                <!-- Tabs Content -->
                <div class="tab-content">

                    <div class="tab-pane pad-btm fade in active" id="bsc-tab-1">
                        <!-- BASIC FORM ELEMENTS -->
                        <!--===================================================-->
                        <form id="form_outfile_input" class="panel-body form-horizontal form-padding">
                            <input type="hidden" name="keyId" id="keyId" value="${outfile.id}" />
                            <input type="hidden" name="updateUnid" id="updateUnid" value="${outfile.updateUnid}" />
                            <input type="hidden" name="curDutyId" id="curDutyId" value=""/>

                            <div class="form-group">
                                <label class="col-md-2 control-label">发文类别<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input type="text" id="dictItem" name="dictItem" class="form-control" value="${outfile.dictItem.name}" disabled>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">发文标题<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input type="text" id="name" name="name" class="form-control" disabled value="${outfile.name}">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">文件编号<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input type="text" id="fileNo" name="fileNo" class="form-control" disabled value="${outfile.fileNo}">
                                </div>
                            </div>

                            <div class="form-group div-Hidden">
                                <label class="col-md-2 control-label">发文时间<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <div class="input-group date">
                                        <input type="text" class="form-control" id="datPulished" name="datPulished" disabled
                                               value=<fmt:formatDate pattern="yyyy-MM-dd" value="${outfile.datPulished}" /> >
                                        <span class="input-group-addon"><i class="ti-calendar"></i></span>
                                    </div>
                                </div>
                            </div>


                            <div class="form-group">
                                <label class="col-md-2 control-label">通知对象<i class="fa fa-asterisk txt-color-red"></i>
                                    <span id="noticeTypeSpan" style="display: none">${outfile.noticeType}</span>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="noticeType" name="noticeType" class="form-control" disabled>
                                </div>
                            </div>

                            <div class="form-group" id="divNoticeDepts">
                                <label class="col-md-2 control-label">
                                    通知部门
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="deptNames" name="deptNames" class="form-control" disabled
                                           value="<c:forEach var="item" items="${outfile.noticeDepartments}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                </div>
                            </div>

                            <div class="form-group" id="divNoticeUsers">
                                <label class="col-md-2 control-label">
                                    通知个人
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="userNames" name="userNames" class="form-control" disabled
                                           value="<c:forEach var="item" items="${outfile.noticeUsers}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                </div>
                            </div>


                            <div class="form-group">
                                <label class="col-md-2 control-label">
                                    会签对象
                                    <i class="fa fa-asterisk txt-color-red"></i>
                                </label>
                                <div class="col-md-6">
                                    <input type="text" id="viewerNames" name="viewerNames" class="form-control" disabled
                                           value="<c:forEach var="item" items="${outfile.viewers}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">相关附件<i class="fa fa-asterisk txt-color-red"></i></label>
                                <div class="col-md-6">
                                    <input name="uploadify" id="filename" style="display: none"  placeholder="" type="file" >
                                    <input name="fileId" id="fileId" style="display: none" value="${fileId}">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-2 control-label">提交人</label>
                                <div class="col-md-2">
                                    <p class="form-control-static">${outfile.creater.name}</p>
                                </div>
                                <label class="col-md-2 control-label">提交日期</label>
                                <div class="col-md-2">
                                    <p class="form-control-static"><fmt:formatDate pattern="yyyy-MM-dd" value="${outfile.dateApply}" /></p>
                                </div>
                            </div>

                            <c:if test='${(outfile.processState=="Running" || outfile.processState=="Backed") && stepNo !=1 && stepNo !=0}'>
                                <div class="form-group" id="comment">
                                    <label class="col-md-2 control-label">审批意见</label>
                                    <div class="col-md-6">
                                        <textarea name="editor" id="editor" style="height:200px;"></textarea>
                                    </div>
                                </div>
                            </c:if>

                            <div class="flow">
                                <div class="panel-footer">
                                    <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
                                        <i class="fa fa-lg fa-mail-reply"></i> 返回
                                    </a>
                                    <a class="btn btn-default" key="ajax_outfile_edit" href="javascript:void(0);" ${isEdit}>
                                    <i class="fa fa-lg fa-edit"></i> 编辑
                                    </a>
                                    <c:if test="${outfile.processState=='Finished'&&outfile.txtStatus!='已失效'}">
                                        <a class="btn btn-warning" key="ajax_outfile_edit" href="javascript:void(0);"><i class="fa fa-lg fa-minus"></i> 发文修订 </a>
                                    </c:if>
                                </div>

                                <div class="f_title"><i class="right" id="flow"></i>流程信息</div>
                                <div class="f_content" style="display:none">
                                    <div id="showFlow"></div>
                                </div>
                                <div class="f_title"><i class="right" id="next"></i>下一步骤提示</div>
                                <div class="f_content" style="display:none">
                                    <div id="showNext"></div>
                                </div>
                            </div>

                        </form>

                        <!--===================================================-->
                        <!-- END BASIC FORM ELEMENTS -->
                        
                    </div>

                    <div class="tab-pane fade" id="bsc-tab-2">
                        <div class="table-responsive" style="min-height: 530px;margin: 10px">
                            <table id="outfile_history_table"></table>
                            <div id="outfile_history_page"></div>
                        </div>
                    </div>

                </div>
            </div>

        </div>
    </div>
</div>

<script>
    var index = "${index}";
    var todo = "${todo}";
    var remind = "${remind}";
    var record = "${record}";
    var draft =  "${draft}";
    var stepNo = "${stepNo}";

    $(function(){
        loadURL("<%=path%>/com/running/workflow?bussinessId="+$("input#keyId").val()+"&type=flow",$('#showFlow'));
        loadURL("<%=path%>/com/running/workflow?bussinessId="+$("input#keyId").val()+"&type=next",$('#showNext'));
        __ajax_post("<%=path%>/com/running/operateType",{keyId: $("input#keyId").val()},function(pdata){
            var showDuty = false;
            var area = $("form .panel-footer");
            var edit = null;
            if (stepNo == 1){
                return false;
            }
            $(pdata.datarows).each(function(i,v){
                var str = '<a id="left_foot_btn_'+ v.action+'" class="btn btn-warning" href="javascript:void(0);"><i class="fa fa-upload">'+ v.name+'</i></a>&nbsp;';
                $(area).append(str);
                if(v.action == "approve" || v.action=="sendback"  || v.action=="deny"){
                    showDuty = true;
                }
            });
            if(showDuty == true){
                var pdata = {
                    keyId: $("input#keyId").val(),
                    entityName: "com.fz.us.oa.core.entity.com.Outfile"
                };
                multiDuty(pdata);
            }else{
                $('#comment').hide();
            }
            if($(':visible#editor').length == 1){
                edit  = new UE.ui.Editor();
                edit.render('editor');
                /*
                 edit.ready(function() {
                 edit.setContent("");
                 });*/
            }
            //通过
            $("#left_foot_btn_approve").off("click").on("click",function(){
                var comment = "";
                if(edit != null){
                    comment = encodeURIComponent(edit.getContent());
                }
                if($.trim(comment) == ""){
                    alert("请填写审批意见");
                    return false;
                }
                __form_save("form_outfile_input","<%=path%>/com/outfile/approve?comment="+comment,function(){
                    $("#apply-re-common").trigger("click");
                })
            });
            //退回
            $("#left_foot_btn_sendback").off("click").on("click",function(){
                var comment = "";
                if(edit != null){
                    comment = encodeURIComponent(edit.getContent());
                }
                if($.trim(comment) == ""){
                    alert("请填写审批意见");
                    return false;
                }
                var vActionUrl="<%=path%>/com/outfile/back?comment="+comment+"&curDutyId="+$('#curDutyId').val();
                var data={keyId:$("input#keyId").val()};
                __ajax_post(vActionUrl,data,function(pdata){
                    __toastr(pdata);
                    $("#apply-re-common").trigger("click");
                });
            });
            //否决
            $("#left_foot_btn_deny").off("click").on("click",function(){
                var comment = "";
                if(edit != null){
                    comment = encodeURIComponent(edit.getContent());
                }
                if($.trim(comment) == ""){
                    alert("请填写审批意见");
                    return false;
                }
                var vActionUrl="<%=path%>/com/outfile/deny?comment="+comment+"&curDutyId="+$('#curDutyId').val();
                var data={keyId:$("input#keyId").val()};
                __ajax_post(vActionUrl,data,function(pdata){
                    __toastr(pdata);
                    $("#apply-re-common").trigger("click");
                });
            });
        });

        //流程信息展开
        $('#flow,#next').click(function(){
            if($(this).hasClass("right")){
                $(this).removeClass("right").addClass("down");
                $(this).parent(".f_title").next("div.f_content").show();
            }else{
                $(this).removeClass("down").addClass("right");
                $(this).parent(".f_title").next("div.f_content").hide();
            }
        });
    });

    // 附件显示
    readLoad({
        objId:"filename",
        entityName:"fileIds",
        sourceId:"fileId"
    });

    //返回视图
    $("#apply-re-common").click(function(){
        if(index==1){
            loadURL("menu/dashboard",$('#page-content'));
        }else if(todo==1){
            loadURL("menu/toDo",$('#page-content'));
        }else if(remind==1){
            loadURL("menu/remind",$('#page-content'));
        }else if(record==1){
            loadURL("menu/taskRecord?type=1",$('#page-content'));
        }else if(record==2){
            loadURL("menu/taskRecord?type=2",$('#page-content'));
        }else if(draft==1){
            loadURL("menu/draft",$('#page-content'));
        }else{
            returnToHashUrl()
        }
    });

    //编辑
    $("a[key='ajax_outfile_edit']").unbind("click").bind("click",function(){
        loadURL("<%=path%>/com/outfile/input?keyId="+$("#keyId").val()+"&draft="+draft+"&todo="+todo+"&remind="+remind+"&record="+record+"&index="+index+"/n",$('#page-content'));
    });

    // 加载完成后执行
    $(function(){
        var type = $("#noticeTypeSpan").text();
        if (type=="1") {
            $("#noticeType").val("部门");
            $("#divNoticeDepts").show();
            $("#divNoticeUsers").hide();
        } else if(type=="2"){
            $("#noticeType").val("个人");
            $("#divNoticeDepts").hide();
            $("#divNoticeUsers").show();
        } else {
            $("#noticeType").val("全体员工");
            $("#divNoticeDepts").hide();
            $("#divNoticeUsers").hide();
        }

        init_grid_outfile_history();
        jQuery("#outfile_history_table").jqGrid('setGridWidth',  $("#bsc-tab-1").width()-66);
    })

    function init_grid_outfile_history() {
        var parentId=$("#updateUnid").val();
        if(parentId==""){
            parentId=$("#keyId").val();
        }
        __init_jqgrid('outfile_history_table', 'outfile_history_page',
            '<%=path%>/com/outfile/list?updateUnid='+parentId,
            ['发文类别', '文件编号', '发文标题', '发文时间', '修订次数','文档状态', '操作', 'id'],
            [
                {name: 'dictItemName', index: 'dictItemName', width: 50, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
                {name: 'fileNo', index: 'fileNo', width: 50, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
                {name: 'name', index: 'name', width: 200, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'datPulished', index: 'datPulished', width: 50, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
                {name: 'numRevise', index: 'numRevise', width: 50, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
                {name: 'txtStatus', index: 'txtStatus', width: 50, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
                {name: 'act', index: 'act', width: 150, fixed: true, sortable: false, search: false, formatter: actFormat},
                {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,true
        );

    }

    function actFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        cellvalue = '<button class="btn btn-white btn-sm" data-original-title="查看" onclick="fn_outfile_read(\'' + id + '\');"><i class="fa fa-eye"> </i> 查看</button> ';
        return cellvalue;
    }

    function fn_outfile_read(id) {
        var url = '<%=path%>/com/outfile/readhistory/'+id;
        __open_dialog_form_read('发文申请表', url);
    }
</script>
